package com.xyhua.webconfig.config;


import com.xyhua.webconfig.config.web.MyFilter;
import com.xyhua.webconfig.config.web.MyHttpSessionListener;
import com.xyhua.webconfig.config.web.MyInterceptor;

import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.Charset;
import java.util.List;

/**
 * description: WebConfig <br>
 * date: 2020/3/23 3:48 下午 <br>
 * author: huaxiansheng <br>
 * version: 1.0 <br>
 *
 */

/**
 * 在spring boot的自定义配置类继承 WebMvcConfigurationSupport 后，发现自动配置的静态资源路径
 * （classpath:/META/resources/，classpath:/resources/，classpath:/static/，classpath:/public/）不生效。
 * 指定默认的静态资源的位置
 * @Override
 *  protected void addResourceHandlers(ResourceHandlerRegistry registry) {
 *   registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
 *   super.addResourceHandlers(registry);
 *  }
 *
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {




    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/");
        super.addResourceHandlers(registry);
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }

    private HttpMessageConverter<?> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }


    /**
     * 增加拦截器
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/asd/**");
        super.addInterceptors(registry);
    }
    /**
     * 过滤器
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public FilterRegistrationBean mFilterRegistrationBean() {
        FilterRegistrationBean<MyFilter> myFilterFilterRegistrationBean = new FilterRegistrationBean<>();
        MyFilter myFilter = new MyFilter();
        myFilterFilterRegistrationBean.setFilter(myFilter);
        myFilterFilterRegistrationBean.setOrder(1);
        myFilterFilterRegistrationBean.addUrlPatterns("/");
        return myFilterFilterRegistrationBean;
    }


    /**
     * 监听器
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public ServletListenerRegistrationBean mServletListenerRegistrationBean() {
        ServletListenerRegistrationBean<MyHttpSessionListener> myHttpSessionListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
        myHttpSessionListenerServletListenerRegistrationBean.setListener(new MyHttpSessionListener());
        return myHttpSessionListenerServletListenerRegistrationBean;
    }
}
